<?php
class Merchant_Model_Product
{
	protected $_table;
	
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_Product();
	}
	
	public static function getInstance(){
		return new Merchant_Model_Product();
	}
	
	/**
	* 获得dbtable对象
	* @return Zend_Db_Table_Abstract
	*/
	public function getDbTable(){
		return $this->_table;
	}
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	/**
	* 检查sku是否存在
	* @param string $sku
	* @param int $customerId
	* @return boolean
	*/
	public function getByCustomerIdAndSku($customerId,$sku){
		$select = $this->_table->select()->where("customer_id = ?",$customerId)->where("product_sku = ?",$sku);
		return  $this->_table->fetchRow($select);
	}
	
	public function getByProductId($id){
		$select = $this->_table->select()->where('product_id = ?', $id);
	    if($rowset = $this->_table->fetchRow($select)){
			return $rowset->toArray();
		}	
		return false;	
	}
	
	/**
	* 分页查找
	* @param array $condition
	* @param int $page
	* @param int $pageSize
	* @param int $customerId
	* @return boolean
	*/
	public function getAll($condition = array(),$page,$pageSize,$customerId){
		if($page < 1) $page = 1;
		if($pageSize < 1) $pageSize = 1;
		$dbAdapter = $this->_table->getAdapter();
		$where = $dbAdapter->quoteInto("customer_id = ?",$customerId);
		if(isset($condition['productSku']) && $condition['productSku'] != ""){
			$where .= $dbAdapter->quoteInto(" and product_sku = ?",$condition['productSku']);
		}
		if(isset($condition['productStatus']) && $condition['productStatus'] != ""){
			$where .= $dbAdapter->quoteInto(" and product_status = ?",$condition['productStatus']);
		}
		if(isset($condition['product_title']) && $condition['product_title'] != ""){
			$where .= $dbAdapter->quoteInto(" and product_title = ?",$condition['product_title']);
			 
		} 
				
		$order = "";
		if(isset($condition['sortColumn']) && $condition['sortColumn'] != ""){
			if($condition['sortColumn'] == "productName"){
				$order .= "product_title";
			}
			if($condition['sortColumn'] == "sku"){
				$order .= "product_sku";
			}
			if($condition['sortColumn'] == "status"){
				$order .= "product_status";
			}
			if($condition['sortColumn'] == "weight"){
				$order .= "product_weight";
			}
			if($condition['sortColumn'] == "time"){
				$order .= "product_date_added";
			}
			
		}else{
			$order .= " product_date_added ";
		}
		
		if(isset($condition['sortMethod']) && $condition['sortMethod'] != ""){
			$order .= " ".$condition['sortMethod'];
		}else{
			$order .= " desc";
		}
//		echo $order;exit;
		$rows = $this->_table->fetchAll($where,$order,$pageSize,($page-1)*$pageSize);		
		return $rows;
	}
	
	/**
	* 获取记录总数
	* @param array $condition
	* @param $customerId
	* @return int
	*/
	public function getCount($condition=array(),$customerId){
		$select = $this->_table->getAdapter()->select()->from($this->_table->info("name"),"count(*)")->where("customer_id = ?",$customerId);
		if(isset($condition['productSku']) && $condition['productSku'] != ""){
			$select->where("product_sku = ?",$condition['productSku']);
		}
		if(isset($condition['productStatus']) && $condition['productStatus'] != ""){
			$select->where("product_status = ?",$condition['productStatus']);
		}
		if(isset($condition['product_title']) && $condition['product_title'] != ""){
			$select->where("product_title = ?",$condition['product_title']);
		} 
		 
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
	/**
	* 更改产品状态
	* @param int $productId
	* @param int $status
	* @return boolean
	*/
	public function changeProductStatus($productId,$status){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		return $this->_table->update(array("product_status"=>$status,'product_last_modified'=>date('Y-m-d H:i:s')),$where);
	}
	
	/**
	* 获得产品
	* @param int $productId
	* @param int $customerId
	* @return zend_db_table_row
	*/
	public function getProduct($productId,$customerId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		$where .= $this->_table->getAdapter()->quoteInto(" and customer_id = ?",$customerId);
		return $this->_table->fetchRow($where);
	}
	
	/**
	* 获得产品
	* @param int $sku
	* @param int $customerId
	* @return zend_db_table_row
	*/
	public function getProductBySku($sku,$customerId){
		$where = $this->_table->getAdapter()->quoteInto("product_sku = ?",$sku);
		$where .= $this->_table->getAdapter()->quoteInto(" and customer_id = ?",$customerId);
		return $this->_table->fetchRow($where);
	}
	
	/**
	* 获得产品
	* @param int $sku
	* @param int $customerId
	* @return zend_db_table_row
	*/
	public function getProductBySkus($skus,$customerId){
		$where = $this->_table->getAdapter()->quoteInto("product_sku  in(?)",$skus);
		$where .= $this->_table->getAdapter()->quoteInto(" and customer_id = ?",$customerId);
//		echo $where;exit;
		
		$result =  $this->_table->fetchAll($where);
		if($result instanceof Zend_Db_Table_RowSet){
			return $result->toArray();
		}	
		return $result;
	}
		
	/**
	* 获得产品包材重量
	* @param array $customerId
	* @return float
	*/
	public function getProductPackageWeightByProductId($productIds){		
		$productIds = implode(",",$productIds);
		$sql = "select MAX(pp_weight)  from product_package a,product_package_map b,product c where c.product_id in( $productIds) and b.package_id = a.pp_id and c.product_id = b.product_id";
//		echo $sql;
		return $this->_table->getAdapter()->fetchOne($sql);
	}
	
	//获取最后一个产品的ID
	public function getMaxProductId(){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(product_id)');
		return $this->_table->getAdapter()->fetchOne($select);	
	}
}